package cst.a722wr.zyfypt_722wr.Adapter;


import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cst.a722wr.zyfypt_722wr.Bean.FocusResult;
import cst.a722wr.zyfypt_722wr.Bean.UserBean;
import cst.a722wr.zyfypt_722wr.R;

public class FocusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater inflater;
    private List<FocusResult<UserBean>> list;
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public FocusAdapter(Context context)
    {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setList(List<FocusResult<UserBean>> l){
        list = l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.item_user, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        UserBean bean = list.get(position).getBean();
        if(bean == null) return;
        final ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.item_head.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.tware_default));
        viewHolder.username.setText(bean.getUsername());
        viewHolder.rolename.setText(bean.getRolename());
        viewHolder.realname.setText(bean.getRealname());
        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(viewHolder.itemView, position);
                }
            });
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(viewHolder.itemView, position);
                    return false;
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView username;
        TextView rolename;
        TextView realname;
        ImageView item_head;

        public ViewHolder(View itemView) {
            super(itemView);
            item_head = (ImageView) itemView.findViewById(R.id.item_head);
            username = (TextView) itemView.findViewById(R.id.item_username);
            rolename = (TextView) itemView.findViewById(R.id.item_rolename);
            realname = (TextView) itemView.findViewById(R.id.item_realname);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
